<?php
include 'seguranca.php';
require_once 'Model/DAO/AnimalDAO.php';
require_once 'Model/Animal.php';
protegePagina();

if (isset($_POST) && !empty($_POST)) {
	$nome       = addslashes($_POST['nome']);
	$raca       = addslashes($_POST['cboRaca']);
	$tipoAnimal = addslashes($_POST['cboTipo']);
	$sexo       = addslashes($_POST['sexo']);
	$pelagem    = addslashes($_POST['pelagem']);
	$nasc       = addslashes($_POST['nasc']);
	$municipio  = addslashes($_POST['municipio']);
	$peso       = addslashes($_POST['peso']);
	$video      = addslashes($_POST['video']);
	$obs        = addslashes($_POST['obs']);
	$filiacao   = addslashes($_POST['cboFiliacao']);

	// Propriedades do Arquivo
	$tiposPermitidos  = array('image/gif','image/jpeg','image/pjpeg','image/png' );
	$tamanhoPermitido = 1024 * 1024 * 2; // 2MB

	$arqName     = $_FILES['foto']['name'];
	$arqType     = $_FILES['foto']['type'];
	$arqSize     = $_FILES['foto']['size'];
	$arqTemp     = $_FILES['foto']['tmp_name'];
	$arqError    = $_FILES['foto']['error'];

	if ($arqError == 0)
	{
		if (array_search($arqType, $tiposPermitidos) === FALSE)
		echo 'O tipo de arquivo é inválido';
		elseif ($arqSize > $tamanhoPermitido)
		echo 'O tamanho do arquivo e maior do que o permitido';
		else{
			$pasta    = 'uploads/';
			$extensao = strtolower(end(explode('.', $arqName)));

			// Define o novo nome do arquivo
			$foto = time() . '.' . $extensao;

			// Preenche animal
			$animal = new Animal();
			$animal->setNomeAnimal($nome);
			$animal->setFoto($foto);
			$animal->setRaca($raca);
			$animal->setTipoAnimal($tipoAnimal);
			$animal->setSexoAnimal($sexo);
			$animal->setPelagemAnimal($pelagem);
			$animal->setDataNasc($nasc);
			$animal->setMunicipio($municipio);
			$animal->setPeso($peso);
			$animal->setVideo($video);
			$animal->setObservacao($obs);
			$animal->setFiliacao($filiacao);

			$animalDAO = new AnimalDAO();
			$upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
			
			switch ($categoria) {
				case 'Fazenda':
				  $upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
				break;
				
				case 'Gir':
				  $upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
				break;
				
				case 'Guzera':
				 $upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
				break;
				
				case 'Simental':
				 $upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
				break;
				
				case 'Pampa':
				 $upload = move_uploaded_file($arqTemp, $pasta . $foto . $arqName);
				break;
				default:
					;
				break;
			}
			 
			if($upload == true)
			{
				$animalDAO->addAnimal($animal);
				if ($animalDAO)
				echo 'Animal cadastrado com sucesso';
			}
		}
	}
	else
	echo 'Ocorreu um erro tente novamente';
}
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='pt-br' lang='pt-br'>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
<meta http-equiv='content-language' content='pt-br' />
<meta name='robots' content='noindex,nofollow' />
<link rel='stylesheet' media='screen,projection' type='text/css'
				href='css/reset.css'
/>
<link rel='stylesheet' media='screen,projection' type='text/css'
				href='css/main.css'
/>
<link rel='stylesheet' media='screen,projection' type='text/css'
				href='css/2col.css' title='2col'
/>
<link rel='alternate stylesheet' media='screen,projection' type='text/css'
				href='css/1col.css' title='1col'
/>
<!--[if lte IE 6]><link rel='stylesheet' media='screen,projection' type='text/css' href='css/main-ie6.css' /><![endif]-->
<!-- MSIE6 -->
<link rel='stylesheet' media='screen,projection' type='text/css'
				href='css/style.css'
/>
<link rel='stylesheet' media='screen,projection' type='text/css' href='css/mystyle.css'/>
<script type='text/javascript' src='http://www.shiguenori.com/jquery/jquery-1.3.1.js"></sc'></script>

></script>
<script type='text/javascript' src='js/fileUpload.js'></script>
<script type='text/javascript'>
            $(document).ready(function(){
            	$("#foto").fileUpload({
            	      'uploader': 'includes/uploader.swf',
            	      'cancelImg': 'img/cancel.png',
            	      'folder': 'uploads',
            	      'script': 'upload.php',
            	      'fileDesc': 'Image Files',
            	      'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            	      'multi': true,
            	      'auto':  true
            	      
            	   });          
            });
          
        </script>
        <style>
/* CSS PARA ESTILIZAR A BARRA DE PROGRESSO */
body {
   font: 12px/18px Arial, Helvetica, sans-serif;
}
.fileUploadQueueItem {
   font: 11px Verdana, Geneva, sans-serif;
   background-color: #F5F5F5;
   border: 3px solid #E5E5E5;
   margin-top: 5px;
   padding: 10px;
   width: 300px;
}
.fileUploadQueueItem .cancel {
   float: right;
}
.fileUploadProgress {
   background-color: #FFFFFF;
   border-top: 1px solid #808080;
   border-left: 1px solid #808080;
   border-right: 1px solid #C5C5C5;
   border-bottom: 1px solid #C5C5C5;
   margin-top: 10px;
   width: 100%;
}
.fileUploadProgressBar {
   background-color: #0099FF;
}
</style>
        
		<title>Fotos</title>
</head>
<body>
<div id='main'>
	<?php 
	   include 'includes/inc_header.php';
	   include 'includes/inc_menu.php'; 
	?> 
	<div id='cols' class='box'>
		<?php include 'includes/left-colum-cad.php' ?>
	</div>
	<div id='content' class='box'>
		<h1>Cadastro de fotos</h1>
		<p class='msg info' id='resposta'>Para inserir as fotos selecione primeiro a categoria</p>
		<p class='msg error' id='error'></p>
		<form action='' method='post' id='frmGaleria' enctype='multipart/form-data'>
			<fieldset>
				<legend>Animal</legend>
			
				<p class='nomt'>
					<label for='cboRaca' class='req'>Selecione a categoria: *</label>
					<select name='cboRaca' id='cboRaca'>
						<option value='Nenhum'>Nenhum</option>
						<option value='Fazenda'>Fazenda</option>
						<option value='Gir'>Gir Leiteiro</option>
						<option value='Guzerá'>Guzerá</option>
						<option value='Simental'>Simental</option>
						<option value='Pampa'>Pampa</option>
					</select>
				</p>
				<p class='nomt'>
					<label for='foto' class='req'>Foto: *</label><br />
					<input type='file' 
						   name='foto' 
						   id='foto'
						   title='Escolha uma foto'/>
						   
				    <a href="javascript:$('#foto').fileUploadStart()">Começar upload</a> | <a href="javascript:$('#foto').fileUploadClearQueue()">Limpar Fila</a>
				    
				</p>
</fieldset>
<p><input type='submit' id="enviaAnimal" value='Cadastrar foto' /></p>
</form>
</div>
</div>
<hr class='noscreen' />
<?php include 'includes/inc_footer.php'; ?>